package com.diploma.interaction.controller;

import com.diploma.interaction.model.WxxcxDlzhxx;
import com.diploma.interaction.service.QxDlzhxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/v1/login")
@Slf4j
public class LoginController {

    @Autowired
    QxDlzhxxService qxDlzhxxService;

    @RequestMapping("/notVerify")
    @ResponseBody
    String notVerify() {
        return "username or password NOT correct";
    }

    @RequestMapping("/login")
    String login(Model model) {
        model.addAttribute("staff", new WxxcxDlzhxx());
        return "login";
    }

    @RequestMapping(value = "/staffLogin", method = RequestMethod.POST)
    String staffLogin(WxxcxDlzhxx staff, Model model, HttpSession session) {
        boolean verify = verifyStaff(staff);
        if (verify) {
            model.addAttribute("id", staff.getSwryDm());
            model.addAttribute("password", staff.getDlzhkl());
            session.setAttribute("staff",staff);
            return "index";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }
    public boolean verifyStaff(WxxcxDlzhxx staff) {

        if (qxDlzhxxService.selectByStaffIdAndPassword(staff.getSwryDm(),staff.getDlzhkl()).isEmpty()) {
            return false;
        } else {
            return true;
        }

    }
}
